package 
{
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.display.StageDisplayState;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import FLVPlayback_fla.MainTimeline;
	//import flash.media.Video;
	//import flash.net.NetConnection;
	//import flash.net.NetStream;
	import flash.net.URLRequest;
	import fl.video.FLVPlayback;

	/**
	 * ...
	 * @author amyflash.com
	 */
	[Frame(factoryClass="Preloader")]
	public class Main extends Sprite 
	{

		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}

		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			//stage.displayState = StageDisplayState.FULL_SCREEN;
			loadAll();
		}
		
		private var player:FLVPlayback;
		private var _closebtn:closebtn;
		private var _playerbg:Sprite;
		private var _videotitle:TextField;
		private function loadAll():void
		{
			//loadbg();
			loadslideshow();
			loadrotation();
			
			loadVideoConfig('Config/videodata.xml');
			
			
			
            //player.skinBackgroundColor = 0x666666;
            //player.skinBackgroundAlpha = 0.5;
		}
		private function loadVideoConfig(s:String):void
		{
			var _urlloader:URLLoader = new URLLoader();
			var _urlr:URLRequest = new URLRequest(s);
			_urlloader.load(_urlr);
			_urlloader.addEventListener(Event.COMPLETE, exec,false,0,true);
		}
		
		private var _b1_url:String;
		private var _b2_url:String;
		private var _b3_url:String;
		
		private var _v1_url:String;
		private var _v2_url:String;
		private var _v3_url:String;
		
		private var _v1_title:String;
		private var _v2_title:String;
		private var _v3_title:String;
		private function exec(e:Event):void
		{
			var t:XML = XML(e.target.data);
			_b1_url = t.video[0].@btnurl;
			_v1_title = t.video[0].@title;
			_b2_url = t.video[1].@btnurl;
			_v2_title = t.video[1].@title;
			_b3_url = t.video[2].@btnurl;
			_v3_title = t.video[2].@title;
			
			_v1_url = t.video[0].@url;
			_v2_url = t.video[1].@url;
			_v3_url = t.video[2].@url;
			
			initVideo();
			loadvideobtns();
		}
		private function initVideo():void
		{
			_playerbg = new Sprite();
			_playerbg.graphics.beginFill(0x000000);
			addChild(_playerbg);
			_playerbg.visible = false;
			
			_videotitle = new TextField();
			_videotitle.text = "";
			
			_videotitle.visible = false;
			
			var _tf:TextFormat = new TextFormat();
			_tf.color = 0xffffff;
			_videotitle.defaultTextFormat = _tf;
			
			player = new FLVPlayback();
			addChild(player);
			player.visible = false;
			player.width = 400;
			player.height = 300;
			player.x = (this.stage.stageWidth - 400) / 2;
			player.y = (this.stage.stageHeight - 300) / 2;
			_playerbg.graphics.drawRect(player.x, player.y, 400, 300);
			_playerbg.graphics.endFill();
			_videotitle.x = player.x;
			_videotitle.y = player.y;
			addChild(_videotitle);
			
			_closebtn = new closebtn();
			addChild(_closebtn);
			_closebtn.x = player.x + 400 - _closebtn.width;
			_closebtn.y = player.y;
			_closebtn.visible = false;
			_closebtn.buttonMode = true;
			_closebtn.addEventListener(MouseEvent.CLICK, doCloseVideo, false, 0, true);
		}
		private function doCloseVideo(e:MouseEvent):void
		{
			player.visible = false;
			player.stop();
			_closebtn.visible = false;
			_playerbg.visible = false;
			_videotitle.visible = false;
		}
		
		private function setPlayer(s:String,t:String):void
		{
			player.source = s;
			_videotitle.text = t;
			player.play();
			player.visible = true;
			_playerbg.visible = true;
			_closebtn.visible = true;
			_videotitle.visible = true;
			player.width = 400;
			player.height = 300;

		}
		
	/*	private function loadbg():void
		{
			var _bgloader:Loader = new Loader();
			_bgloader.load(new URLRequest('bg.swf'));
			addChild(_bgloader);
		}*/
		
		private function loadslideshow():void
		{
			var _bgloader:Loader = new Loader();
			_bgloader.load(new URLRequest('slides.swf'));
			addChild(_bgloader);
			//var sp:Sprite = new Sprite();
			//sp.addChild(_bgloader);
			//addChild(sp);
			
			//_bgloader.contentLoaderInfo.addEventListener(Event.COMPLETE, resize,false,0,true);
		//	_bgloader.x = (this.stage.stageWidth - 400) / 2;
			_bgloader.y = (this.stage.stageHeight - 300) / 2;
		}
		
		
		private function loadrotation():void
		{
			var _rloader:Loader = new Loader();
			_rloader.load(new URLRequest('rotation.swf'));
			addChild(_rloader);
			_rloader.x = this.stage.stageWidth - 300;
			_rloader.y = 20;
		}
		
		private function loadvideobtns():void
		{
			var b1:Loader = new Loader();
			b1.load(new URLRequest(_b1_url));
			addChild(b1);
			b1.x = (this.stage.stageWidth - 605) / 2+30;
			b1.y = this.stage.stageHeight -150;
			
			var b2:Loader = new Loader();
			b2.load(new URLRequest(_b2_url));
			addChild(b2);
			b2.x = b1.x + 225;
			b2.y = b1.y;
			
			var b3:Loader = new Loader();
			b3.load(new URLRequest(_b3_url));
			addChild(b3);
			b3.x = b2.x + 225;
			b3.y = b1.y;
			
			b1.addEventListener(MouseEvent.CLICK, showv1, false, 0, true);
			b2.addEventListener(MouseEvent.CLICK, showv2, false, 0, true);
			b3.addEventListener(MouseEvent.CLICK, showv3, false, 0, true);
			/*var video:Video = new Video();
			addChild(video);
			 
			var nc:NetConnection = new NetConnection();
			nc.connect(null);
			 
			var ns:NetStream = new NetStream(nc);
			ns.client = {onMetaData:ns_onMetaData, onCuePoint:ns_onCuePoint};
			 
			video.attachNetStream(ns);
			ns.play("Assets/videos/1.flv");
			 
			function ns_onMetaData(item:Object):void {
			 //trace("metaData");
			 // Resize video instance.
			 video.width = item.width;
			 video.height = item.height;
			 trace(video.width,video.height);
			 // Center video instance on Stage.
			 video.x = (stage.stageWidth - video.width) / 2;
			 video.y = (stage.stageHeight - video.height)/2;
			}
			 
			function ns_onCuePoint(item:Object):void {
			 //trace("cuePoint");
			 //trace(item.name + "\t" + item.time);
			}*/
		}
		
		private function showv1(e:MouseEvent):void
		{
			setPlayer(_v1_url,_v1_title);
		}
		
		private function showv2(e:MouseEvent):void
		{
			setPlayer(_v2_url,_v2_title);
		}
		
		private function showv3(e:MouseEvent):void
		{
			setPlayer(_v3_url,_v3_title);
		}
	}

}